'use strict';

const Controller = require('egg').Controller;

class NuewsController extends Controller {
  // 查询所有用户
  async findall(){
    const {ctx,service}=this;
    const payload=ctx.request.body ||{};
    console.log(payload);
    const res=await service.user.findall(payload);
    var code=200;
    ctx.body={res,date:{code}};
  }
//创建新的用户
  async create() {
    const{ctx,service}=this;
    const payload=ctx.request.body ||{};
    console.log(payload)
    const res=await service.user.create(payload);
    
    var code=200;
    ctx.body={res,data:{code}};
  }
//用户登录
  async login(){
    const {ctx,service}=this;
    const payload=ctx.request.body ||{};

    const userData=await service.user.login(payload.name);
    
    console.log(userData)

    if(userData.password === payload.password){
        var code=200;
        
        ctx.body={userData, date:{code}}
        
    }else{
      var code=500;
      console.log(code);
        ctx.body={date:{code}};
    }
  }
//人名查找
  async findname() {
    const {ctx,service}=this;
    const _id=ctx.query;//获取路由参数
    const res=await service.user.findname(_id);
    if(res&&res!=""){
      ctx.body={
        code:200,
        data:res
      }
    }else{
      ctx.body='没有'
    }
  }
//修改信息
  async update(){
    const {ctx,service}=this;
    const _id=ctx.query  ;
    const payload=ctx.request.body ||{};
    const res=await service.user.update(_id,payload);
    ctx.body={
      code:200,
      msg:"修改完成",
      data:res
    }
  }
//删除用户 正确的
  async delete(){
    const{ctx,service}=this;
    const _id =ctx.query;
  
    const res= await service.user.delete(_id);
    ctx.body={
      code:200,
      msg:"删除成功",
      data:res
    }
  }
  }


module.exports = NuewsController;
